package com.itheima.web.controller;


import com.github.pagehelper.PageInfo;
import com.itheima.constant.MessageConstant;
import com.itheima.domain.Course;
import com.itheima.domain.Result;
import com.itheima.factory.BeanFactory;
import com.itheima.service.CourseService;
import com.itheima.utils.BeanUtil;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/store/course/*")
public class CourseServlet extends BaseServlet {
    private CourseService courseService = BeanFactory.getBean("courseService", CourseService.class);
    private Result delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
        try {
            String[] ids = BeanUtil.fillBeanFromJson(request, String[].class);

            courseService.delete(ids);
            return new Result(true, MessageConstant.DELETE_COURSE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_COURSE_FAIL);
        }
    }

    private Result edit(HttpServletRequest request, HttpServletResponse response) throws IOException {

        try {
            Course course = BeanUtil.fillBeanFromJson(request, Course.class);

            courseService.update(course);
            return new Result(true, MessageConstant.EDIT_COURSE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_COURSE_FAIL);
        }
    }

    private Result findById(HttpServletRequest request, HttpServletResponse response) throws IOException {

        try {
            String id = request.getParameter("id");

            Course course = courseService.findById(id);
            return new Result(true, MessageConstant.QUERY_COURSE_SUCCESS, course);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_COURSE_FAIL);
        }
    }

    private Result save(HttpServletRequest request, HttpServletResponse response) throws IOException {

        try {
            Course course = BeanUtil.fillBeanFromJson(request, Course.class);

            courseService.save(course);
            return new Result(true, MessageConstant.SAVE_COURSE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.SAVE_COURSE_FAIL);
        }
    }

    private Result findByPage(HttpServletRequest request, HttpServletResponse response) throws IOException {

        try {
            String currentPageStr = request.getParameter("currentPage");
            String pageSizeStr = request.getParameter("pageSize");
            int currentPage = 1, pageSize = 5;
            if (!StringUtils.isBlank(currentPageStr)) {
                currentPage = Integer.parseInt(currentPageStr);
            }
            if (!StringUtils.isBlank(pageSizeStr)) {
                pageSize = Integer.parseInt(pageSizeStr);
            }

            PageInfo<Course> info = courseService.findByPage(currentPage, pageSize);
            return new Result(true, MessageConstant.QUERY_COURSE_SUCCESS, info);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_COURSE_FAIL);
        }

    }
    private Result findAll(HttpServletRequest request, HttpServletResponse response) throws IOException {

        try {
            List<Course> list = courseService.findAll();
            return new Result(true, MessageConstant.QUERY_COURSE_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_COURSE_FAIL);
        }

    }

}
